﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Globalization;

namespace EPMAuditing.EventHandlers.RegisterEvents
{
    public class EnrolledEventCollection : IEnumerable, IDisposable
    {
        private List<EnrolledEvent> _enrolledEvents;

        public EnrolledEventCollection()
        {
            _enrolledEvents = new List<EnrolledEvent>();
        }

        ~EnrolledEventCollection()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                _enrolledEvents.Clear();
            }
        }

        public IEnumerator GetEnumerator()
        {
            return _enrolledEvents.GetEnumerator();
        }


    }

    public class EnrolledEvent
    {
        private bool _isEnabledInitial;
        private bool _isEnabled;

        private string _assemblyName;
        private string _className;
        private string _description;
        private Guid _eventHandlerUid;
        private int _eventHandlerId;
        private string _eventName;
        private int _eventHandlerOrder;


        // for adding an eventstate when not already registered with Project Server
        public EnrolledEvent(string assemblyName, ProjectServerEvent projectServerEvent, bool isEnabledInitial, bool isEnabled)
        {
            _assemblyName = assemblyName;
            _className = projectServerEvent.ClassName;
            _description = "Used by the EPM Auditing solution for capture information about this event";
            _eventHandlerUid = Guid.NewGuid(); //TODO: Should we have standard Guids?
            _eventHandlerId = projectServerEvent.EventId;
            _eventName = projectServerEvent.EventName;
            _eventHandlerOrder = 0;  //TODO: Is this the correct value here?
            _isEnabledInitial = isEnabledInitial;
            _isEnabled = isEnabled;
        }

        // for adding an eventstate when not already registered with Project Server
        public EnrolledEvent(string assemblyName, string className, string eventName, int eventHandlerId,
                             bool isEnabledInitial, bool isEnabled)
        {
            _assemblyName = assemblyName;
            _className = className;
            _description = "Used by the EPM Auditing solution for capture information about this event";
            _eventHandlerUid = Guid.NewGuid(); //TODO: Should we have standard Guids?
            _eventHandlerId = eventHandlerId;
            _eventName = eventName;
            _eventHandlerOrder = 0;  //TODO: Is this the correct value here?
            _isEnabledInitial = isEnabledInitial;
            _isEnabled = isEnabled;
        }

        // for adding an eventstate when read from Project Server (i.e. already registered)
        public EnrolledEvent(string assemblyName, string className, string description, Guid eventHandlerUid,
                             int eventHandlerId, string eventName, int eventHandlerOrder,
                             bool isEnabledInitial, bool isEnabled)
        {
            _assemblyName = assemblyName;
            _className = className;
            _description = description;
            _eventHandlerUid = eventHandlerUid;
            _eventHandlerId = eventHandlerId;
            _eventName = eventName;
            _eventHandlerOrder = eventHandlerOrder;
            _isEnabledInitial = isEnabledInitial;
            _isEnabled = isEnabled;
        }

        public EnrolledEvent Copy()
        {
            return new EnrolledEvent(_assemblyName, _className, _description, _eventHandlerUid, _eventHandlerId,
                                    _eventName, _eventHandlerOrder, _isEnabledInitial, _isEnabled);
        }

        public EventsWS.EventHandlersDataSet.EventHandlersRow EventHandlersRow(EventsWS.EventHandlersDataSet eventHandlersDataSet)
        {
            EventsWS.EventHandlersDataSet.EventHandlersRow ehRow =
                eventHandlersDataSet.EventHandlers.NewEventHandlersRow();
            ehRow.AssemblyName = _assemblyName;
            ehRow.ClassName = ClassNameToRegister;
            ehRow.Description = _description;
            ehRow.EventHandlerUid = _eventHandlerUid;
            ehRow.EventId = _eventHandlerId;
            ehRow.Name = _eventName;
            ehRow.Order = _eventHandlerOrder;
            return ehRow;
        }

        //private int EventHandlerOrder
        //{
        //    get
        //    {
        //        EventsWS.EventHandlersDataSet ds = events.ReadEventHandlerAssociationsForEvent((EventsWS.PSEventID)_eventHandlerId);
        //        foreach (DataRow dr in ds.EventHandlers.Rows)
        //        {
        //            order = Math.Max(order, (int)dr["Order"]);
        //        }
        //        return ++order;
        //    }
        //}



        public string ClassName
        {
            get { return _className; }
        }

        public string ClassNameToRegister
        {
            get { return string.Format(CultureInfo.InvariantCulture, "Microsoft.Office.Project.EPMAuditing.EventHandlers.{0}Events", _className); }
        }

        public string EventName
        {
            get { return _eventName; }
        }

        public Guid EventHandlerUid
        {
            get { return _eventHandlerUid; }
        }

        public bool IsEnabledInitial
        {
            get { return _isEnabledInitial; }
        }

        public bool IsEnabled
        {
            get { return _isEnabled; }
            set { _isEnabled = value; }
        }

        public bool IsChanged
        {
            get
            {
                if (_isEnabledInitial == _isEnabled)
                    return false;
                else
                    return true;
            }
        }

        public void ResetInitialEnabled()
        {
            _isEnabledInitial = _isEnabled;
        }
    }

    public class ProjectServerEvent
    {
        private string _eventName;

        public string EventName
        {
            get { return _eventName; }
        }

        private string _className;
        public string ClassName
        {
            get { return _className; }
        }

        private int _eventId;
        public int EventId
        {
            get { return _eventId; }
        }

        public ProjectServerEvent(string eventName, string className, int eventId)
        {
            _eventName = string.Format(CultureInfo.InvariantCulture, "{0}.{1}", className, eventName);
            _className = className;
            _eventId = eventId;
        }
    }

}
